package com.mxf.example.web.job;

import com.alibaba.fastjson.JSONObject;
import com.mxf.example.exception.BizException;
import com.mxf.example.utils.http.Result;
import com.mxf.example.utils.job.QuartzJobManager;
import com.mxf.example.utils.tools.PackageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/job", produces = "application/json;charset=UTF-8")
@Api(value = "定时任务相关接口", tags = {"定时任务相关接口"})
public class JobController {
    @Autowired
    QuartzJobManager quartzJobManager;


    @GetMapping("/addJob")
    @ApiOperation(value = "创建任务", notes = "创建任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupName", value = "任务组名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "任务名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cron", value = "cron表达式", required = true, dataType = "String"),
            @ApiImplicitParam(name = "className", value = "任务类完整名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "args", value = "json格式的参数", required = true, dataType = "String"),
    })
    public Result<String> addJob(String groupName, String jobName, String cron, String className, String args) {
        try {
            quartzJobManager.addJob(Class.forName(className), jobName, groupName, cron, JSONObject.parseObject(args, Map.class));
            return Result.createSuccess(jobName + "任务创建启动成功");
        } catch (ClassNotFoundException e) {
            throw new BizException("初始化" + className + "任务失败");
        }
    }

    @GetMapping("/updateJob")
    @ApiOperation(value = "更新任务", notes = "更新任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobGroupName", value = "任务组名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "jobName", value = "任务名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cronExpression", value = "cron表达式", required = true, dataType = "String")
    })
    public Result<String> updateJob(String jobName, String jobGroupName, String cronExpression) {
        try {
            quartzJobManager.updateJob(jobName, jobGroupName, cronExpression);
            return Result.createSuccess(jobName + "任务更新成功");
        } catch (Exception e) {
            throw new BizException(jobName + "任务更新失败");
        }
    }

    @GetMapping("/deleteJob")
    @ApiOperation(value = "删除任务", notes = "删除任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobGroupName", value = "任务组名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "jobName", value = "任务名称", required = true, dataType = "String")
    })
    public Result<String> deleteJob(String jobName, String jobGroupName) {
        try {
            quartzJobManager.deleteJob(jobName, jobGroupName);
            return Result.createSuccess(jobName + "任务删除成功");
        } catch (Exception e) {
            throw new BizException(jobName + "任务删除失败");
        }
    }

    @GetMapping("/getClassName")
    @ApiOperation(value = "获取package下所有类名", notes = "获取package下所有类名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "packageName", value = "包名", required = true, dataType = "String")
    })
    public Result<List<String>> getClassName(String packageName) {
        try {
            return Result.createSuccess(PackageUtil.getClassName(packageName));
        } catch (IOException e) {
            throw new BizException("获取任务类失败");
        }
    }

    @GetMapping("/pauseJob")
    @ApiOperation(value = "暂停任务", notes = "暂停任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobGroupName", value = "任务组名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "jobName", value = "任务名称", required = true, dataType = "String")
    })
    public Result<String> pauseJob(String jobName, String jobGroupName) {
        try {
            quartzJobManager.pauseJob(jobName, jobGroupName);
            return Result.createSuccess(jobName + "任务暂停成功");
        } catch (Exception e) {
            throw new BizException(jobName + "任务暂停失败");
        }
    }

    @GetMapping("/resumeJob")
    @ApiOperation(value = "恢复任务", notes = "恢复任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobGroupName", value = "任务组名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "jobName", value = "任务名称", required = true, dataType = "String")
    })
    public Result<String> resumeJob(String jobName, String jobGroupName) {
        try {
            quartzJobManager.resumeJob(jobName, jobGroupName);
            return Result.createSuccess(jobName + "任务启动成功");
        } catch (Exception e) {
            throw new BizException(jobName + "任务启动失败");
        }
    }

    @GetMapping("/getAllJob")
    @ApiOperation(value = "获取所有任务", notes = "获取所有任务")
    public Result<List<Map<String, Object>>> getAllJob() {
        return Result.createSuccess(quartzJobManager.getAllJob());
    }

    @GetMapping("/shutdownAllJobs")
    @ApiOperation(value = "关闭所有任务(不可恢复)", notes = "关闭所有任务(不可恢复)")
    public Result<String> shutdownAllJobs() {
        try {
            quartzJobManager.shutdownAllJobs();
            return Result.createSuccess("关闭所有任务成功");
        } catch (Exception e) {
            throw new BizException("关闭所有任务失败");
        }
    }

    @GetMapping("/standAllJobs")
    @ApiOperation(value = "暂停所有任务", notes = "暂停所有任务")
    public Result<String> standAllJobs() {
        try {
            quartzJobManager.standAllJobs();
            return Result.createSuccess("暂停所有任务成功");
        } catch (Exception e) {
            throw new BizException("暂停所有任务失败");
        }
    }

    @GetMapping("/startAllJobs")
    @ApiOperation(value = "启动所有任务", notes = "启动所有任务")
    public Result<String> startAllJobs() {
        try {
            quartzJobManager.startAllJobs();
            return Result.createSuccess("启动所有任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("启动所有任务失败");
        }
    }
}
